package cn.gson.crm.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.ClassifyDao;
import cn.gson.crm.model.dao.InveDao;

import cn.gson.crm.model.dao.ProductDao;
import cn.gson.crm.model.mapper.ProductMapper;
import cn.gson.crm.model.pojo.ClassifyProductTb;

import cn.gson.crm.model.pojo.InveTb;
import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.vo.ClassifyProductTbVo;

import cn.gson.crm.vo.ProductVo;

@Service
@Transactional

public class ProductService {
	@Autowired
	private ProductDao proDAO;
@Autowired
private ClassifyDao claDAO;
@Autowired
private ProductMapper ProMAP;
@Autowired
private InveDao inDao;

	/**
	 * 获取产品的所有数据
	 */
	public List<ProductVo> getAll() {
		try {
			List<ProductVo> listproVo = new ArrayList<>();
			List<Product> listproPo = (List<Product>) proDAO.findAll();

			if (listproPo != null) {
				for (Product p : listproPo) {

					ClassifyProductTb classPo = p.getClassifyProductTb();
	
					InveTb invePo = p.getInveTb();

					ProductVo proVo = new ProductVo(p.getProductId(), p.getProInfoCost(), p.getProPrice(),
							p.getUnitId(), p.getProInfoSta(), p.getProSta(), p.getProductName(),
							 invePo.getInveProNumb(),classPo.getClassifyName());
					listproVo.add(proVo);
				}

			}
			return listproVo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
/**
 * 获取产品分类
 */
public List<ClassifyProductTbVo> getAllClassify(){
	try {
		List<ClassifyProductTbVo>listVo=new ArrayList<>();
		List<ClassifyProductTb>listPo=(List<ClassifyProductTb>)claDAO.findAll();
		if(listPo!=null){
			for(ClassifyProductTb p:listPo){
				ClassifyProductTbVo classVo = new ClassifyProductTbVo(p.getClassifyId(),
						p.getClassifyName());
				listVo.add(classVo);
			}
		}
		return listVo;
	}catch (Exception e) {
		e.printStackTrace();
		throw new ServiceException(e);
	}
}
	/**
	 * 
	 * 分页查询
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<ProductVo> getAllProsBypage(int page, int rows) {
		try {
			PageRequest p = new PageRequest(page - 1, rows);// 第一个参数是第几页，第二个参数是多少个
			List<Product> listPo = proDAO.queryByPage(p);
			List<ProductVo> listVo = Po2VoList(listPo);
			return listVo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 分页模糊查询产品所有信息
	 * 
	 * @return
	 */
	public List<ProductVo> getAllProsBypageWhere(int page, int rows, String key) {
		try {
			PageRequest p = new PageRequest(page - 1, rows);
			List<Product> listPo = proDAO.findByProductNameLike("%" + key + "%", p);
			List<ProductVo> listVo = Po2VoList(listPo);
			return listVo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	public List<ProductVo> getAllProsBypageWhere1(String key,Long key1) {
		try {
	
			List<Product> listPo = ProMAP.findAll("%" + key + "%",key1);
			List<ProductVo> listVo = Po2VoList(listPo);
			return listVo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	public List<ProductVo> getAllProsBypageWhere2(String key) {
		try {
			List<Product> listPo = proDAO.findByProInfoSta(key);
			List<ProductVo> listVo = Po2VoList(listPo);
			return listVo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	public List<ProductVo> findsomepro(Product poo,String prosta33){
		System.out.println(poo.getProductName());
		System.out.println(prosta33);
		BigDecimal hu=new BigDecimal(1);
	
		if(prosta33.equals("正常")){
			
			hu=new BigDecimal(0);
		}
		poo.setProSta(hu);
		List<Product> listPo= ProMAP.findSome(poo);
		List<ProductVo> listVo = Po2VoList(listPo);
		return listVo;
	}
	public void delpro(String kkk){
		Long idid=Long.valueOf(kkk);

		proDAO.delete(idid);
		
	}


	private List<ProductVo> Po2VoList(List<Product> listproPo) {
		List<ProductVo> listproVo = new ArrayList<>();
		if (listproPo != null) {
			for (Product p : listproPo) {
				System.out.println(p);
				ClassifyProductTb classPo = p.getClassifyProductTb();
				InveTb invePo = p.getInveTb();
				ProductVo proVo = new ProductVo(p.getProductId(), p.getProInfoCost(), p.getProPrice(), p.getUnitId(),
						p.getProInfoSta(), p.getProSta(), p.getProductName(),
						invePo.getInveProNumb(),classPo.getClassifyName());
			
				listproVo.add(proVo);
			}
		}
		return listproVo;
	}

public void addNewPro(Product pro,String proSta1,String claa){
	try {
		ClassifyProductTb cls= claDAO.findOneByClassifyName(claa);

		InveTb intt=new InveTb();
		intt.setInveProNumb(new BigDecimal(0));
		inDao.save(intt);
	
			if(cls!=null){
				if(proSta1.equals("正常")){
				pro.setProSta( new BigDecimal(0));}else{
					pro.setProSta( new BigDecimal(1));
				}
				pro.setClassifyProductTb(cls);
				pro.setInveTb(intt);
				proDAO.save(pro);
			}else{
				ClassifyProductTb cls1=new ClassifyProductTb();
				cls1.setClassifyName(claa);
				claDAO.save(cls1);
				if(proSta1.equals("正常")){
					pro.setProSta( new BigDecimal(0));}else{
						pro.setProSta( new BigDecimal(1));
					}
					pro.setClassifyProductTb(cls1);
					pro.setInveTb(intt);
					proDAO.save(pro);
			}
	
	} catch (Exception e) {
		// TODO: handle exception
	}
} 
public void uptproduct(Product pro,String proSta12,String claa){
	try{
		if(proSta12.equals("正常")){
			
			pro.setProSta(new BigDecimal(0));
		}else{
			pro.setProSta(new BigDecimal(1));
		}
		Long proid=pro.getProductId();
		Product hhhh=proDAO.findOneByProductId(proid);
		InveTb assa=hhhh.getInveTb();
		if(claa==""){
			ClassifyProductTb sasa=hhhh.getClassifyProductTb();
			pro.setClassifyProductTb(sasa);
			pro.setInveTb(assa);
			proDAO.save(pro);
		}else{
			ClassifyProductTb cls= claDAO.findOneByClassifyName(claa);
			if(cls!=null){
				pro.setClassifyProductTb(cls);
				pro.setInveTb(assa);
				proDAO.save(pro);
			}else{
				ClassifyProductTb cls1=new ClassifyProductTb();
				cls1.setClassifyName(claa);
				claDAO.save(cls1);
					pro.setClassifyProductTb(cls1);
					pro.setInveTb(assa);
					proDAO.save(pro);
			}
		}
	}catch (Exception e) {
		// TODO: handle exception
	}
}
	public long count() {
		return proDAO.count();
	}
	public long countwhere(String key){
		return proDAO.countwhere("%"+key+"%");
	}

}
